package shell.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import shell.view.windows.RulesPanel;
/**@author Rafael Rocha*/
public class SelectRuleListener implements ActionListener, CaretListener{
	private JComboBox searchTypeBox;
	private JTextField searchField;
	private RulesPanel panel;
	
	public SelectRuleListener(RulesPanel panel, JTextField searchField, JComboBox searchTypeBox) {
		this.panel = panel;
		this.searchField = searchField;
		this.searchTypeBox = searchTypeBox;
	}

	private void update() {
		String typeOfSearch = searchTypeBox.getSelectedItem() == null?null : searchTypeBox.getSelectedItem().toString();
		
		if(typeOfSearch == null){return;}
		
		if(typeOfSearch.equals(RulesPanel.BY_RULE_ID)) {
			panel.updateRulesPanelById(searchField.getText());
		}else if(typeOfSearch.equals(RulesPanel.BY_RULE_BODY)) {
			panel.updateRulesPanelByBodyText(searchField.getText());
		}else if(typeOfSearch.equals(RulesPanel.BY_VARIABLE)) {
			panel.updateRulesPanelByVariable(searchField.getText());
		}
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		update();
	}

	@Override
	public void caretUpdate(CaretEvent e) {
		update();
	}
}
